package com.zhou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zhou.constant.MessageConstant;
import com.zhou.entity.PageResult;
import com.zhou.entity.QueryPageBean;
import com.zhou.entity.Result;
import com.zhou.pojo.MemberFileReport;
import com.zhou.service.MemberUploadService;
import com.zhou.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

@RestController
@RequestMapping("/memberupload")
public class MemberUploadController {
    @Reference
    private MemberUploadService memberUploadService;
    //分页查
    @PostMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult findpage=memberUploadService.findByPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return findpage;
    }
    //上传文件
    @GetMapping("/findById")
    public Result findById(Integer id) {
        try {
            MemberFileReport memberFileReport = memberUploadService.findById(id);
            return new Result(true, MessageConstant.QUERY_CMEMBERFILEREPORT_SUCCESS, memberFileReport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBERFILEREPORT_FAIL);
        }
    }
    @RequestMapping("/uploadFile")
    public Result upload(@RequestParam("file") MultipartFile file){
        try {
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            int i = originalFilename.lastIndexOf(".");
            //获取后缀
            String substring = originalFilename.substring(i - 1);
            //UUID随机生成文件名
            String s = UUID.randomUUID().toString() + substring;
            QiniuUtils.upload2Qiniu(file.getBytes(),s);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(s);
            return result;
        }catch (Exception e){
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    @GetMapping("/deleteFileReport")
    public Result delete(Integer id){
        try {
            memberUploadService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
    @PostMapping("/uploadFileReport")
    public Result uploadFileReport(@RequestBody MemberFileReport memberFileReport) {
        try {
            return memberUploadService.uploadFileReport(memberFileReport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBERFILEREPORT_FAIL);
        }
    }
}